let mCurPage = 1
let mMaxLimit = 5

$(function(){
    $('body > .mdui-progress').fadeOut(200);

    let dog = new $.dog();

    $("#more-comment-btn").bind("click", function(){ load(mCurPage, mMaxLimit) })
    $("#more-comment-btn").hide()
    load(mCurPage, mMaxLimit);

    function load(pageNum, pageLimit){
        $("#more-comment-btn").hide()
        $("#more-article-loading").show()
        dog.get({
            url: '/api/v1/article/',
            data: {
                page: pageNum,
                limit: pageLimit,
                category : $('#hideCategory').val(),
                search : $('#hideSearch').val()
            },
            success: function (result) {
                $("#more-article-loading").hide();
                if (result.errCode != 0) {
                    mdui.snackbar({
                        message: result.errDesc + "(" + result.errCode + ")",
                        timeout: 5000,
                        position: 'top',
                    });
                    return;
                }

                loadSuccess(result.data);
            },
            error: function (error) {
                $("#more-article-loading").hide();
                mdui.snackbar({
                    message: error || '数据加载失败',
                    timeout: 5000,
                    position: 'top',
                });
            }
        });
    }

    function loadSuccess(data){
        mCurPage = data.nextPage;
        let hasNextPage = data.hasNextPage;
        let rows = data.rows;

        if (rows==null || rows==0){
            addPost();
            $("#more-comment-btn").hide();
            return
        }

        rows.forEach(function(post){
            addPost(post);
        })

        if(hasNextPage){
            $("#more-comment-btn").show();
        }else{
            $("#more-comment-btn").hide();
        }
    }

    function addPost(article){
        if (article == null){
            let html = '<i class="mdui-icon material-icons mdui-center mdx-search-empty">&#xe880;</i>'+
            '<h1 class="mdui-center mdx-search-empty-text mdx-search-empty-title" style="text-align: center;">这里似乎空空如也</h1>'+
            '<h2 class="mdui-center mdx-search-empty-text mdx-search-empty-end"></h2>'
            $("#mainList").append(html);
            return
        }

        if (article.Cover){
            let html = `<div class="mdui-card postDiv mdui-center mdui-hoverable">
                            <div class="mdui-card-media mdui-color-theme">
                                <a onclick="openArticle('` + article.ID + `')" style="cursor:pointer">
                                    <img src="/static/blog/image/loading.gif" data-src='/` + article.Cover +`' alt="` + article.Cover+`" title="` + article.Title + `" class="LazyLoadList mdui-color-theme mdui-text-color-theme lazyload">
                                    <div class="mdui-card-media-covered mdui-card-media-covered-gradient">
                                        <div class="mdui-card-primary">
                                            <div class="mdui-card-primary-title">` + article.Title +`</div>
                                        </div>
                                    </div>
                                </a>
                            </div>
                            <div class="mdui-card-actions">
                                <p class="ct1-p mdui-text-color-black">` + article.Introduction + ` </p>
                                <div class="mdui-divider underline"></div>
                                <span class="info">&nbsp;&nbsp;
                                    <i class="mdui-icon material-icons info-icon">&#xe0cb;</i> 0</a>&nbsp;&nbsp;
                                    <i class="mdui-icon material-icons info-icon">&#xe192;</i>`+ article.Time + `&nbsp;&nbsp;
                                    <i class="mdui-icon material-icons info-icon"></i>
                                </span>
                                <a class="mdui-btn mdui-ripple mdui-ripple-white coun-read mdui-text-color-theme-accent" href="/article/` + article.ID + `">阅读更多</a>
                            </div>
                        </div>`;
            $("#mainList").append(html);
        }else{
            let html = `<div class="mdui-card postDiv mdui-center mdui-hoverable">
                            <div class="mdui-card-primary">
                                <div class="mdui-card-primary-title mdui-typo-title mdui-text-color-indigo-300" onclick="openArticle('` + article.ID + `')" style="cursor:pointer">` + article.Title + `</div>
                            </div>
                            <div class="mdui-card-actions">
                                <p class="ct1-p mdui-text-color-black">`+ article.Introduction +`</p>
                                <div class="mdui-divider underline"></div>
                                <span class="info">&nbsp;&nbsp;
                                    <i class="mdui-icon material-icons info-icon">&#xe0cb;</i> 0</a>&nbsp;&nbsp;
                                    <i class="mdui-icon material-icons info-icon">&#xe192;</i> `+article.Time +`&nbsp;&nbsp;
                                    <i class="mdui-icon material-icons info-icon"></i>
                                </span>
                                <a class="mdui-btn mdui-ripple mdui-ripple-white coun-read mdui-text-color-theme-accent" href="/article/` + article.ID + `" >阅读更多</a>
                            </div>
                        </div>`;
            $("#mainList").append(html);
        }
    }
})

function openArticle(id) {
    window.location.href = "/article/" + id; //在原有窗口打开
}

function refresh(){
    let dog = new $.dog();
    dog.get({
        url: 'api/v1/global/retrieval/',
        success: function (result) {
            mdui.snackbar({
                message: result.errDesc || "加载成功!",
                timeout: 5000,
                position: 'top',
            });
        },
        error: function (result) {
            mdui.snackbar({
                message: result.errDesc + "(" + result.errCode + ")",
                timeout: 5000,
                position: 'top',
            });
        }
    });
}